package cn.xuchunh.campusbbs.utils;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.Toast;

import cn.xuchunh.campusbbs.AppContext;
import cn.xuchunh.campusbbs.R;
import cn.xuchunh.campusbbs.ui.activities.LoginActivity;
import cn.xuchunh.campusbbs.ui.activities.MainActivity;
import cn.xuchunh.campusbbs.net.HttpRequestManager;
import cn.xuchunh.campusbbs.storage.sp.SharedPreferenceManager;

/**
 * Created by XuChunH on 2015/9/12.
 */
public class UIHelper {

    /**
     * 隐藏软键盘
     */
    public static void hideSoftInput(Context context) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context
                .INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.RESULT_UNCHANGED_SHOWN, InputMethodManager
                .HIDE_NOT_ALWAYS);
    }

    public static void showServerConfigDialog(final Activity activity) {
        View contentView = activity.getLayoutInflater().inflate(R.layout.layout_setting_server,
                null);
        final EditText domain = (EditText) contentView.findViewById(R.id
                .server_domain);
        final EditText forumKey = (EditText) contentView.findViewById(R.id
                .app_forumkey);
        final EditText appName = (EditText) contentView.findViewById(R.id.app_name);
        final EditText sdkVersion = (EditText) contentView.findViewById(R.id
                .app_sdk_version);
        final EditText packageName = (EditText) contentView.findViewById(R.id
                .app_package_name);

        final SharedPreferenceManager sharedPreferenceManager = SharedPreferenceManager
                .getInstance();
        domain.setText(sharedPreferenceManager.getServerDomain());
        forumKey.setText(sharedPreferenceManager.getForumKey());
        appName.setText(sharedPreferenceManager.getAppName());
        sdkVersion.setText(sharedPreferenceManager.getSdkVersion());
        packageName.setText(sharedPreferenceManager.getPackageName());

        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("Configuration")
                .setView(contentView)
                .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String serverDomain = domain.getEditableText().toString();
                        String appForumKey = forumKey.getEditableText().toString();
                        String appNameText = appName.getEditableText().toString();
                        String sdkVersionText = sdkVersion.getEditableText()
                                .toString();
                        String packageNameText = packageName.getEditableText()
                                .toString();

                        //这里只做非空的检查，不做格式上的检查
                        if (TextUtils.isEmpty(serverDomain)
                                || TextUtils.isEmpty(appForumKey)
                                || TextUtils.isEmpty(appNameText)
                                || TextUtils.isEmpty(sdkVersionText)
                                || TextUtils.isEmpty(packageNameText)) {
                            Toast.makeText(AppContext.getInstance(), "必须填写所用项目", Toast
                                    .LENGTH_SHORT).show();
                        } else {
                            sharedPreferenceManager.setServerDomain(serverDomain);
                            sharedPreferenceManager.setAppName(appNameText);
                            sharedPreferenceManager.setForumKey(appForumKey);
                            sharedPreferenceManager.setPackageName(packageNameText);
                            sharedPreferenceManager.setSdkVersion(sdkVersionText);
                            dialog.dismiss();
                            HttpRequestManager.getInstance(AppContext.getInstance()).stop();
                            LoginActivity.launch(activity);
                            activity.finish();
                        }
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
    }

    public static void showUIModeDialog(final Activity activity) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        final SharedPreferenceManager preferenceManager = SharedPreferenceManager.getInstance();
        String[] items = {"侧滑", "底部导航"};
        builder.setTitle("主页面样式")
                .setSingleChoiceItems(items, preferenceManager.getUiMode(), new DialogInterface
                        .OnClickListener() {


                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int lastUiMode = preferenceManager.getUiMode();
                        if (lastUiMode != which) {
                            if (lastUiMode == Constants.UI_MODE_BOTTOM
                                    && MainActivity.instance != null) {
                                MainActivity.instance.finish();
                            }
                            preferenceManager.setUiMode(which);
                            LoginActivity.launch(activity);
                            activity.finish();
                        }
                        dialog.dismiss();
                    }
                }).show();
    }

    public static void showCreditsDialog(Activity activity) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        WebView webView = new WebView(activity);
        webView.loadUrl("file:///android_asset/credits.html");
        builder.setTitle("版权信息")
                .setView(webView)
                .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
    }

    public static int dpToPx(float dp, Resources resources){
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics());
        return (int) px;
    }

    public static void setBackground(View view, Drawable d) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            view.setBackground(d);
        } else {
            view.setBackgroundDrawable(d);
        }
    }


}
